/*
    微信小程序的代码组成 是一个基于 node语法 的模块化项目;
    微信小程序集成了node的模块导入和导出功能;
        注意：微信小程序的模块化系统不支持 文件夹 模块
*/
const URL = {
    // 猜你喜欢
    suggestion: "https://m.douban.com/rexxar/api/v2/movie/suggestion",
    // 影院热映 - movie_showing | 免费在线 - movie_free_stream | 近期热门 - movie_latest
    subjectCollection: "https://m.douban.com/rexxar/api/v2/subject_collection/{SUB_URL}/items",
    search: "https://m.douban.com/rexxar/api/v2/search",
    rankList: "https://m.douban.com/rexxar/api/v2/movie/rank_list",
    movieDetail: "https://m.douban.com/rexxar/api/v2/movie/{FILM_ID}",
    movieCredits: "https://m.douban.com/rexxar/api/v2/movie/{FILM_ID}/credits",
    movieComment: "https://m.douban.com/rexxar/api/v2/movie/{FILM_ID}/interests"
};

// 基于 wx.request 的自定义请求工具
const ajaxRequest = function (options = {}) {
    return new Promise(function(resolve, reject){
        options.success = resolve;
        options.fail = reject;
        wx.request(options);
    });
};

// 发送 wx.request 请求 加载 '猜你喜欢' 的数据
const loadSuggestion = function(start=0, count=6){
    return ajaxRequest({
        url: URL.suggestion,
        method: "GET",
        data: {
            start: start, // 从第几条查询
            count: count, // 查询几条
            new_struct: 1,
            with_review: 1,
            for_mobile: 1,
            ck: "sew4"
        }
    });
};

// 影院热映 - movie_showing, 免费在线 - movie_free_stream, 近期热门 - movie_latest
const loadSubjectCollection = function(url="movie_showing", start=0, count=8){
    return ajaxRequest({
        url: URL.subjectCollection.replace("{SUB_URL}", url),
        method: "GET",
        data: {
            start: start, // 从第几条查询
            count: count, // 查询几条
            for_mobile: 1,
            ck: "sew4"
        }
    });
}

// 搜索电影
const loadSearch = function(q="", start=0, count=10){
    return ajaxRequest({
        url: URL.search,
        method: "GET",
        data: {
            q: q,
            // start: start, // 从第几条查询
            // count: count, // 查询几条
            // for_mobile: 1,
            t: "movie" // 限定类型为电影
        }
    });
}

const loadRankList = function(){
    return ajaxRequest({
        url: URL.rankList,
        method: "GET"
    });
}

const loadFilmDetail = function(id=""){
    return ajaxRequest({
        url: URL.movieDetail.replace("{FILM_ID}", id),
        method: "GET",
        data: {
            ck: "sew4",
            for_mobile: 1
        }
    });
}

const loadFilmCredits = function(id="") {
    return ajaxRequest({
        url: URL.movieCredits.replace("{FILM_ID}", id),
        method: "GET",
        data: {
            ck: "sew4",
            for_mobile: 1
        }
    });
}

const loadFilmComment = function(id="", start=0, count=8){
    return ajaxRequest({
        url: URL.movieComment.replace("{FILM_ID}", id),
        method: "GET",
        data: {
            start: start, // 从第几条查询
            count: count, // 查询几条
            order_by: "hot",
            for_mobile: 1,
            ck: "sew4"
        }
    })
}


module.exports = {
    loadSuggestion, loadSubjectCollection, loadSearch,
    loadRankList, loadFilmDetail, loadFilmCredits,
    loadFilmComment
};